package com.ebs.md.api.abs;

/**
 * Revision Author:
 *  $File: //depot/MarketData/commons-md-api/MAIN/src/test/java/com/ebs/md/api/abs/ImplDetailedListener.java $
 *  $Revision: #6 $
 *  $DateTime: 2012/07/24 11:38:02 $
 *  $Change: 112556 $
 *  $Author: Arik.Sher $
 */

import java.util.Calendar;
import java.util.List;

import com.ebs.md.api.IDetailedBookEntry;
import com.ebs.md.api.IIdentifiedOrder;
import com.ebs.md.api.IOrderBook;
import com.ebs.md.api.OrderBookListener;
import com.ebs.md.api.Rate;
import com.ebs.md.api.orderbook.BookEntryPosition;
import com.ebs.md.api.orderbook.BookEntryPositionOfDeleted;


public class ImplDetailedListener
implements OrderBookListener<IOrderBook<IDetailedBookEntry>, IDetailedBookEntry, IIdentifiedOrder>
{
	String beforeAction;
	String  afterAction;
	IDetailedBookEntry afterBE;
	IIdentifiedOrder   afterOrder;
	Rate                afterBidTopRate;
	Rate               beforeBidTopRate;
	Rate                afterOfferTopRate;
	Rate               beforeOfferTopRate;

	Rate                afterBidBestRate;
	Rate               beforeBidBestRate;
	Rate                afterBidWorstRate;
	Rate               beforeBidWorstRate;
	Rate                afterOfferBestRate;
	Rate               beforeOfferBestRate;
	Rate                afterOfferWorstRate;
	Rate               beforeOfferWorstRate;
	
	BookEntryPosition  entryPositionAfterNew;
	BookEntryPositionOfDeleted  entryPositionAfterRemove;
	
	Calendar           afterSnapshotTime;
	boolean            afterSnapshotIsFirst;
	
	protected static Rate getTopBid(IOrderBook<IDetailedBookEntry> ob) {
		List<Rate> topRates = ob.getTopBidRates(1);
		
		if ( topRates.isEmpty() ) {
			return null;
		} else {
			return topRates.iterator().next();
		}
	}
	
	protected static Rate getTopOffer(IOrderBook<IDetailedBookEntry> ob) {
		List<Rate> topRates = ob.getTopOfferRates(1);
		
		if ( topRates.isEmpty() ) {
			return null;
		} else {
			return topRates.iterator().next();
		}
	}
	
	protected void collectBefores(IOrderBook<IDetailedBookEntry> orderbook,	IDetailedBookEntry bookEntry, IIdentifiedOrder order) {
		beforeBidTopRate   = getTopBid  (orderbook);
		beforeOfferTopRate = getTopOffer(orderbook);

		beforeBidBestRate    = orderbook.getBestBid();
		beforeOfferBestRate  = orderbook.getBestOffer();
		
		beforeBidWorstRate   = orderbook.getWorstBid();
		beforeOfferWorstRate = orderbook.getWorstOffer();
	}
	
	protected void collectAfters(IOrderBook<IDetailedBookEntry> orderbook,	IDetailedBookEntry bookEntry, IIdentifiedOrder order) {
		afterBE    = bookEntry;
		afterOrder = order;

		afterBidTopRate   = getTopBid  (orderbook);
		afterOfferTopRate = getTopOffer(orderbook);
		
		afterBidBestRate    = orderbook.getBestBid();
		afterOfferBestRate  = orderbook.getBestOffer();
		
		afterBidWorstRate   = orderbook.getWorstBid();
		afterOfferWorstRate = orderbook.getWorstOffer();
	}

	public void beforeRemoveBookEntry(IOrderBook<IDetailedBookEntry> orderbook,	IDetailedBookEntry bookEntry, IIdentifiedOrder order) {
		beforeAction="RemoveBookEntry";
		collectBefores(orderbook, bookEntry, order);
	}
	
	public void  afterRemoveBookEntry(IOrderBook<IDetailedBookEntry> orderbook,	IDetailedBookEntry bookEntry, IIdentifiedOrder order, BookEntryPositionOfDeleted position) {
		afterAction= "RemoveBookEntry";
		collectAfters(orderbook, bookEntry, order);
		entryPositionAfterRemove = position;
	}

	
	public void beforeModifyBookEntry(IOrderBook<IDetailedBookEntry> orderbook,	IDetailedBookEntry bookEntry, IIdentifiedOrder order) {
		beforeAction="ModifyBookEntry";
		collectBefores(orderbook, bookEntry, order);
	}

	public void  afterModifyBookEntry(IOrderBook<IDetailedBookEntry> orderbook,	IDetailedBookEntry bookEntry, IIdentifiedOrder order) {
		afterAction= "ModifyBookEntry";
		collectAfters(orderbook, bookEntry, order);
	}

	public void beforeNewBookEntry(IOrderBook<IDetailedBookEntry> orderbook,	IDetailedBookEntry bookEntry, IIdentifiedOrder order) {
		beforeAction="NewBookEntry";
		collectBefores(orderbook, bookEntry, order);
	}

	public void  afterNewBookEntry(IOrderBook<IDetailedBookEntry> orderbook,	IDetailedBookEntry bookEntry, IIdentifiedOrder order, BookEntryPosition position) {
		afterAction= "NewBookEntry";
		collectAfters(orderbook, bookEntry, order);
		entryPositionAfterNew = position;
	}
	
	public void beforeSnapshot(IOrderBook<IDetailedBookEntry> orderbook, Calendar snapshotTime, boolean isFirst) {
		beforeAction= "Snapshot";
		collectBefores(orderbook, null, null);
	}

	public void  afterSnapshot(IOrderBook<IDetailedBookEntry> orderbook, Calendar snapshotTime, boolean isFirst) {
		afterAction = "Snapshot";
		collectAfters(orderbook, null, null);
		afterSnapshotTime    = snapshotTime;
		afterSnapshotIsFirst = isFirst;
	}
	
}
